<template>
  <div class="pay">
    <van-nav-bar
      title="支付确认"
      fixed
      placeholder
      z-index="999"
      left-arrow
      @click-left="$router.back()"
    />
    <div class="one">
      <div>当前影城</div>
      <div>{{shopName}}</div>
    </div>
    <div class="two">
      <div>已选商品</div>
      <div>{{info.goodsName}}</div>
    </div>
    <div class="thr">
      <div>价格</div>
      <div>￥{{info.totalPrice}}</div>
    </div>
    <div class="for">支付方式</div>
    <!-- <div class="fiv">
      <svg class="xl-icon" aria-hidden="true" style=" font-size:28px;">
        <use xlink:href="#icon-xyk-copy"></use>
      </svg>
      <div>星意卡</div>
      <div @click="$router.push('/xyk')">
        <span>更多</span>
        <van-icon name="arrow" />
      </div>
    </div>
    <div class="sex">
      <div>
        <van-icon name="wechat" color="#31bb70" size="28" />
      </div>
      <div>
        <div>微信支付</div>
        <div>支持储存卡、信用卡</div>
      </div>
      <div>
        <van-checkbox v-model="checked" checked-color="#ee0a24" @click="checked=true"></van-checkbox>
      </div>
    </div> -->

    <!--  -->

    <van-radio-group v-model="payType">
      <van-cell-group>
        <van-cell title="支付宝" clickable @click="payType = 'alipay'">
          <template #icon>
            <van-icon
              name="alipay"
              color="#027AFF"
              size="24"
              style="margin-right: 3px"
            />
          </template>

          <template #right-icon>
            <van-radio name="alipay" />
          </template>
        </van-cell>
        <van-cell title="微信支付" clickable @click="payType = 'wxpay'">
          <!--  用插槽 把图标插进去 -->

          <template #icon>
            <van-icon
              name="wechat-pay"
              color="#31bb70"
              size="24"
              style="margin-right: 3px"
            />
          </template>

          <template #right-icon>
            <van-radio name="wxpay" />
          </template>
        </van-cell>
      </van-cell-group>
    </van-radio-group>

    <div class="sev">
      <div>待支付:<span>￥{{info.totalPrice}}</span></div>
      <button @click="pay">确认支付</button>
    </div>
  </div>
</template>

<script>
import { RadioGroup, Radio } from "vant";
export default {
  data() {
    return {
      payType: "alipay",
      shopName:'',
      orderSn:'',
      pageShow:true
    };
  },
  components: {
    [RadioGroup.name]: RadioGroup,
    [Radio.name]: Radio,
  },
  computed:{
    info(){
      return this.$route.query||{}
    }
    
  },
  watch:{
     pageShow(n,o){

       if(n){
          this.checkPayOrder()

       }

     }
  },
  created() {

   let {shopName} = localStorage;
   this.shopName = shopName;
    //  验证  是否已经支付成功了 

// console.log()

document.addEventListener('visibilitychange',()=>{

// console.log(document.hidden)

this.pageShow = !document.hidden
},false);
    
    if(this.orderSn){
      //  我们做了假  
      // 只要调用就返回成功
       this.checkPayOrder()
    }

   

    

  },
  methods:{
checkPayOrder(){
let uni_id_token = localStorage.getItem('xl-token');

let orderSn = this.orderSn;
   this.$http.post('/apis/order/kh/checkPayOrder',{
     uni_id_token,
     orderSn
   })
   .then(res=>{
      console.log(res)
      if(res.code == 0){
        //  跳转到支付成功
            let {info:{orderId}} = this;
        this.$router.push(`/paySuccess?orderId=${orderId}`)
      }else{
          this.$router.push('/payFail')
      }
   })

},
    
      
      pay(){
        let uni_id_token = localStorage.getItem('xl-token');
        let {payType,info:{orderId}} = this;
        // console.log(uni_id_token)
        // console.log(payType)
        // console.log(orderId)


          this.$http.post('/apis/order/kh/pay',{
            uni_id_token,
            payType,
            orderId
          })
          .then(res=>{

            console.log(res)

            let orderInfo = res.data.orderInfo;

            this.orderSn = res.data.outTradeNo;

            window.open('https://openapi.alipaydev.com/gateway.do?'+orderInfo)

            // 先验证你的参数对不对   sign  验签 

            // 发拉起支付宝的协议  urlschema    唤起
          
          })

      
      }

  }
};
</script>

<style lang="scss">
.pay {
  .one {
    padding: 0 10px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 40px;
    font-size: 14px;
    border-bottom: 2px solid #e9e9e9;
    div:nth-of-type(2) {
      color: #fc5869;
      font-weight: 700;
    }
  }
  .two {
    padding: 0 10px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 40px;
    font-size: 14px;
    border-bottom: 2px solid #e9e9e9;
    div:nth-of-type(2) {
      font-size: 12px;
      font-weight: 700;
      color: #444;
    }
  }
  .thr {
    padding: 0 10px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 40px;
    font-size: 14px;
    border-bottom: 8px solid #e9e9e9;
    div:nth-of-type(2) {
      font-weight: 700;
    }
  }
  .for {
    padding: 0 10px;
    line-height: 38px;
    font-size: 14px;
  }
  .fiv {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 10px;
    font-size: 14px;
    border-bottom: 2px solid #e9e9e9;
    line-height: 38px;
    div:nth-of-type(1) {
      margin-right: 220px;
    }
  }
  .sex {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 10px;
    font-size: 14px;
    border-bottom: 2px solid #e9e9e9;
    line-height: 22px;
    div:nth-of-type(2) {
      font-size: 12px;
      margin-right: 90px;
      div:nth-of-type(2) {
        color: #aaa;
      }
    }
  }
  .sev {
    width: 100%;
    display: flex;
    justify-content: right;
    align-items: center;
    line-height: 38px;
    position: fixed;
    bottom: 0;
    z-index: 2222;
    background-color: #fff;
    div {
      font-size: 12px;
      span {
        color: #fc5869;
        font-size: 16px;
        padding-right: 10px;
      }
    }
    button {
      width: 100px;
      border: #fff;
      background-color: #fc5869;
      color: #fff;
      font-size: 13px;
    }
  }
}
</style>